#ifndef __SYS_H__
#define __SYS_H__
#include "uart.h"

#define ID_UART_PANEL ID_UART1
#define ID_UART_MOTOR ID_UART2

#define ID_UART_CPSREEN ID_UART3
#define ID_UART_TMIAO   ID_UART0
/**************************************/
#define comm_rx_err       system_errs.bits.bit_0
#define motor_rx_err      system_errs.bits.bit_1
#define shell_leak_err    system_errs.bits.bit_2
#define boxtempr_err      system_errs.bits.bit_3
#define toptempr_err      system_errs.bits.bit_4
#define bomtempr_err      system_errs.bits.bit_5
#define gsnstemper_err    system_errs.bits.bit_6

#define TT_IBOM_OUT        test_out.bits.bit_0
#define TT_OBOM_OUT        test_out.bits.bit_3
#define TT_LBOM_OUT        test_out.bits.bit_4
#define TT_ITOP_OUT        test_out.bits.bit_5
#define TT_OTOP_OUT        test_out.bits.bit_6



/**************************************/
#define BOX_LIGHT_MAX      180 //箱体灯   3min

#define ERR_BOX_NTC        1   //箱体NTC损坏
#define ERR_BOM_NTC        2   //发热盘NTC损坏

#define ERR_MOTOR_RX       5   //电机故障
#define ERR_POWER          6   //过零检测中断一直不进


/**************************************/
typedef enum
{
    STA_NULL = 0,
    STA_WORK,
    STA_APPOINT,
    STA_PAUSE,    
    STA_ALARM,
    
}SteamMode_E; //一定要和通讯协议里的对齐！

typedef enum
{
    ERR_NULL = 0,
    ERR_NTC,
    ERR_HEAT,
    ERR_PANEL_COMM,
    ERR_MOTOR_COMM,

}Error_E; //一定要和通讯协议里的对齐！

typedef enum
{
    GAS_NORMAL= 0,
    GAS_ALARM,
    GAS_ERROR,
} Gas_E;



typedef struct
{
    u8 soft_version; //面板版本号
    u8 fct_version;  //专门给FCT显示的
    u8 device_type;  //设备类型
    u8 buzzer_type;
    u8 smoker_level;

    u8 error;
} DeviceInfo_t;

typedef struct
{
    u8  sruning_task;   //烟机系统状态
    u8  bkruning_task;  //蒸烤箱系统状态
    u16 power_sec;   //开机时间
} SYSTEM_t;

typedef struct
{
    u8 dir_f;        //电机转向
    u8 level;        //档位
    u8 err_code;     //上报的错误代码
	u8 type;
} Smoker_t;

typedef struct
{
    u8 mode0; //档位
    u8 mode1; //控制字节

    s16 speed;
    s16 torque;
    s16 limit_speed;
    s16 limit_power;
} MOTOR_CMD_t;

typedef struct
{
    u8 state;
    u8 err_code;

    s16 cur_power;
    s16 cur_speed;

    s16 ipm_temptr;
    s16 cur_ud;
    s16 cur_uq;

} MOTOR_STATE_t;

typedef struct
{
    Gas_E sta;    //当前状态
    u8 set_level; //设定报警等级
    u8 cur_level; //当前报警等级

} Gas_t;


typedef struct
{
    u8 lock_f; //堵转标志
    u8 cmd;    //面板下发的动作命令 1：on 0:off

    u8 cur_i_10mA;
    u8 lock_10ms;

    u8 motoDly_sec;
} Moto_t;

typedef struct
{
    u8 exist_f;       //存在与否
    u8 dir_f;         //翻转方向
    u8 lock_i_10mA;   //堵转电流
    u8 lock_cfm_10ms; //堵转电流确认时间

    u8 runMax_sec;    //最大运行时间
    u8 runMin_sec;    //最小运行时间
    u8 stopTime_10ms; //切换停止时间

    u8 dly_sec;       //关闭风机是翻版延迟的时间
    u8 motoDly_sec;   //打开翻版时风机延迟的时间

} MotoPara_t;



typedef struct
{
    u8  perc;         //输出百分比
    u16 power;       //输出功率
} Heat_t;

typedef struct
{
    SteamMode_E state;
    u8 powerErr_sec; //过零检测有问题  一直不进中断

} SteamSta_t; //蒸箱状态


typedef enum
{
    WORK_ALLOFF  = 0,
    WORK_STEAMPURE,          // 纯蒸模式(肉类)1
    WORK_FISH,               // 鱼类模式2
    WORK_VEGTAB,             // 蔬菜模式3
    WORK_EGG,          	     // 蛋类模式4
    WORK_NOODLE,     		 // 面食模式5
	WORK_SOUP,		         // 煲汤模式6
    WORK_FAJIAO,             // 发酵模式7
	WORK_THAW,			     // 解冻模式8	
    WORK_SOFT,               // 软化9
    WORK_CHUJUN,	             // 除菌10
    WORK_WASH,               // 清洁
    WORK_DESCAL,             // 除垢
}WorkModeTYPE;
typedef enum
{
    BWORK_ALLOFF  = 0,
    BWORK_MEAT,               // 肉类模式1
    BWORK_FISH,               // 鱼类模式2
    BWORK_STEAK,          	  // 牛排3
	BWORK_PIZZA,			  // 披萨4
    BWORK_CAKE,               // 蛋糕5
    BWORK_TUBERS,             // 薯类6
    BWORK_NUT,                // 果仁7
    BWORK_KEEPWARM,           //保温
	BWORK_CHUJUN,	          // 除菌8
}BakeModeTYPE;

typedef struct
{
    WorkModeTYPE menu; //菜单
    u8 tempr_set;
    u8 tempr_temp;
    u8 time_min;
    u8 time_sec;
    u8 time_temp;
    u8 soft_step;
    
} SteamPara_t; //蒸箱参数

typedef struct
{
    BakeModeTYPE menu; //菜单
    u8 tempr_set;
    u8 time_temp;
    
} BakePara_t; //烤箱参数

typedef struct
{
    u8 cmd_flag; //输出指令
    u8 cap_flag; //电容状态 1:电量满的   0：等待充满
    u8 off_time;
    u8 work_time;
} Fireoff_t; 

/**************************************/

extern u8               WALL_LEVEL;
extern u8				start_buzzcnt;

extern DeviceInfo_t     devi_info;

extern SYSTEM_t         system;
extern Bit8_ut          system_errs;	

extern UART_t           panel_comm;
extern UART_t           motor_comm;
extern UART_t		    screen_comm;

extern Smoker_t        smoker;		
extern MOTOR_CMD_t     motor_cmd;
extern MOTOR_STATE_t   motor_state;

extern Moto_t  		   board;				
extern MotoPara_t      boardPara;


extern SteamSta_t       steamCtrl;
extern SteamPara_t      steamPara;

extern SteamSta_t       bakeCtrl;
extern BakePara_t		bakePara;

extern Heat_t			ht_Lbom_out; // 烤箱顶部加热管//左加热盘
extern Heat_t			ht_Otop_out; //蒸箱顶部加热管500W//上外
extern Heat_t			ht_Itop_out; //蒸盘外550W//上内
extern Heat_t			ht_Obom_out; //无//蒸盘外
extern Heat_t			ht_Ibom_out; //蒸盘内2000W//蒸盘内
extern Heat_t			ht_pre_out;

extern Fireoff_t        fireoff_L;
extern Fireoff_t        fireoff_R;


//照明
extern bool             light_f ;     // 照明灯
extern bool             atmos_f ;     //氛围灯
extern bool             boxLight_f ;  //箱体灯
extern bool             boxlock_f;
extern bool             smokerpower_f ;
extern bool             klight_f;
extern bool             klock;
extern bool			    zpre12V_f;
extern bool			    kpre12V_f;
//除菌
extern bool             cycle_f;		//循环
extern bool             cold_f;     	//冷却

extern u8			    buzz_mode;       //蜂鸣器
extern bool             cut_stove_f; //点火器
extern bool             top_keepwarm_f;
extern bool			    wash_f;
extern Gas_t            gasCtrl;
extern u8               gu8_SystemBeginTime;
extern Long_ut 		    eeprom_rec;
extern bool             gb_start;
extern u16              worktime;
extern bool			    gb_realtemper;
extern u8 			    gu8_version;
extern bool			    gb_ganscheckon;
extern bool             gb_firesmallon;
extern bool			    gb_washboxwork;
extern crom u8         wall_tempr[5];

/**************************************/
void process_packet(UART_t * c_uart);

void comm_err_ctrl(void);
void device_err_ctrl(void);


void send_panel_comm(u8 cmd);
void send_motor_comm(void);

//void config_eeprom_para(bool set_f);
void system_init(void);

#endif

